import time

# 记日志
def log(fun):
    print('log_start...')
    def _dec2():
        print(fun.__name__, 'called.')
        return fun()
    print('log_end...')
    return _dec2

# 计算执行时间
def cost(fun):
    print('cost_start...')
    def _dec1():
        print('start...')
        starttime = time.time()
        result = fun()   # 执行被装饰的函数
        costtime = time.time()-starttime
        print('cost:', costtime, 'seconds' )
        return result
    print('cost_end')
    return _dec1

#
# # 应用2个装饰器（log/cost）
@log
@cost
def add():
    print('I am add.')
    time.sleep(1)
    result = 1+2
    return result

# 使用装饰器
result = add()
print(result)
#
# result = log(cost(add))()
# print(result)